package com.hex.ds.hdrs.app.controller;


import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.app.dto.AppConfDto;
import com.hex.ds.hdrs.app.po.AppConf;
import com.hex.ds.hdrs.app.service.impl.AppConfService;
import com.hex.ds.hdrs.app.vo.AppSessionVO;
import com.hex.ds.hdrs.common.page.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/appDef")
@Api(tags = "应用管理")
public class AppConfController {

    @Autowired
    private AppConfService appConfService;

    /**
     * 修改应用状态
     *
     * @param appSatus
     * @param appCode
     * @return
     */
    @ApiOperation("/修改应用状态")
    @PostMapping("/editAppSatus")
    @Log("应用状态修改")
    public ResponseEntity<Object> editAppSatus(@RequestParam String appSatus, @RequestParam String appCode) {
        log.debug("【进入AppConfController的editAppSatus】", appSatus);
        this.appConfService.editAppSatus(appSatus, appCode);
        return new ResponseEntity("修改成功", HttpStatus.OK);
    }

    /**
     * 应用管理信息展示
     *
     * @param appConf
     * @param page
     * @return
     */
    @ApiOperation("应用信息展示")
    @GetMapping("/selectPages")
    @Log("应用信息展示")
    public ResponseEntity<Object> selectPages(AppConf appConf, Page page) {
        log.debug("【进入AppConfController的selectPages】", appConf.toString());
        PageData data = new PageData(appConfService.selectPages(appConf, page), page);
        return new ResponseEntity(data, HttpStatus.OK);
    }

    /**
     * 应用信息添加
     *
     * @param appConfDto
     * @return
     */
    @ApiOperation("应用信息添加")
    @PostMapping("/add")
    @Log("应用信息添加")
    public ResponseEntity<Object> add(@RequestBody AppConfDto appConfDto) {
        log.debug("【进入AppConfController的add】", appConfDto.toString());
        this.appConfService.add(appConfDto);
        return new ResponseEntity("新增成功", HttpStatus.OK);
    }

    /**
     * 应用删除
     *
     * @param list
     * @return
     */
    @ApiOperation("应用删除")
    @PostMapping("/del")
    @Log("应用删除")
    public ResponseEntity<Object> del(@RequestBody List<String> list) {
        log.debug("【进入AppConfController的del】的参数: {}", list.toString());
        this.appConfService.del(list);
        return new ResponseEntity("删除成功", HttpStatus.OK);
    }

    @ApiOperation("获取某个应用信息")
    @GetMapping("/getEdit")
    public ResponseEntity<Object> getEdit(@RequestParam String pkId) {
        log.debug("【进入AppConfController的getEdit】==》{}", pkId);
        AppConfDto appConfDto = this.appConfService.getEdit(pkId);
        return new ResponseEntity(appConfDto, HttpStatus.OK);
    }

    @ApiOperation("应用修改")
    @PostMapping("/edit")
    @Log("应用信息修改")
    public ResponseEntity<Object> edit(@RequestBody AppConfDto appConfDto) {
        log.debug("【进入AppConfController的edit】==》{}", appConfDto.toString());
        this.appConfService.edit(appConfDto);
        return new ResponseEntity("修改成功", HttpStatus.OK);
    }


    @ApiOperation("应用配置修改")
    @PostMapping("/updateAppConf")
    public ResponseEntity<Object> updateAppConf(@RequestBody AppConfDto appConfDto) {
        log.debug("【进入AppConfController的updateAppConf】==》{}", appConfDto.toString());
        this.appConfService.updateAppConf(appConfDto);
        return new ResponseEntity("修改成功", HttpStatus.OK);
    }

    @ApiOperation("code和Name查询")
    @GetMapping("/qrycodename")
    public ResponseEntity<Object> qryCodeName() {
        log.info("【AppConfController的qryCodeName】的参数");
        List<AppConf> list = this.appConfService.qryCodeName();
        return new ResponseEntity(list, HttpStatus.OK);
    }

    /**
     * 查询字段是否重复
     *
     * @param appConf
     * @return
     */
    @PostMapping("/checkAppData")
    @ApiOperation("判断是否重复")
    public ResponseEntity<Object> checkAppData(@RequestBody AppConf appConf) {
        log.info("【AppGroupConfController的checkAppData】=》{}", appConf.toString());
        boolean result = this.appConfService.checkAppData(appConf);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @ApiOperation("通过appCode查询应用组件信息")
    @GetMapping("/findAppConfDtoByAppCode")
    public ResponseEntity<Object> findAppConfDtoByAppCode(@RequestParam @NotBlank String appCode) {
        log.info("【AppConfController的findAppConfByAppCode】的参数");
        AppConfDto appConfDto = this.appConfService.findAppConfDtoByAppCode(appCode);
        return new ResponseEntity(appConfDto, HttpStatus.OK);
    }

    @ApiOperation("获取当前应用的基本信息")
    @GetMapping("/getCurrentAppInf")
    public ResponseEntity<Object> getCurrentAppInf(@RequestParam @NotBlank String appCode) {
        log.info("【AppConfController的getCurrentAppInf】");
        AppConfDto appConfDto = this.appConfService.getCurrentAppInf(appCode);
        return new ResponseEntity(appConfDto, HttpStatus.OK);
    }

    @Log("查询内存有效应用")
    @ApiOperation("查询内存有效应用")
    @GetMapping(value = "/qryAppSession")
    public ResponseEntity<Object> qryAppSession(AppSessionVO appSession, Page page) {
        PageData pageData = new PageData(this.appConfService.qryAppSession(appSession, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("查询内存无效应用")
    @ApiOperation("查询内存无效应用")
    @GetMapping(value = "/qryNotValidAppSession")
    public ResponseEntity<Object> qryNotValidAppSession(AppSessionVO appSession, Page page) {
        PageData pageData = new PageData(this.appConfService.qryNotValidAppSession(appSession, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @Log("查询内存应用map")
    @ApiOperation("查询内存应用map")
    @GetMapping(value = "/qryAppSessionMap")
    public ResponseEntity<Object> qryAppSessionMap(AppSessionVO appSession, Page page) {
        PageData pageData = new PageData(this.appConfService.qryAppSessionMap(appSession, page), page);
        return new ResponseEntity(pageData, HttpStatus.OK);
    }

    @AnonymousAccess
    @Log("查询内存应用所有信息")
    @ApiOperation("查询内存应用所有信息")
    @GetMapping(value = "/qryAppSessionInfo/{appCode}")
    public ResponseEntity<Object> qryAppSessionInfo(@PathVariable("appCode") String appCode) {
        return new ResponseEntity(appConfService.qryAppSessionInfo(appCode), HttpStatus.OK);
    }
}

